package com.lc.zpyh.ui.activity.mine;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.zpyh.R;
import com.lc.zpyh.app.AppActivity;
import com.lc.zpyh.http.glide.GlideApp;
import com.lc.zpyh.http.request.CancelAttentionApi;
import com.lc.zpyh.http.request.PublicMsgBean;
import com.lc.zpyh.http.request.UserWatchListApi;
import com.lc.zpyh.http.response.UserWatchListBean;
import com.lc.zpyh.other.IntentKey;
import com.lc.zpyh.ui.activity.supermarket.BusinessActivity;
import com.lc.zpyh.util.SPUtil;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class ShopFollowActivity extends AppActivity {
    @BindView(R.id.rv_follow)
    RecyclerView rvFollow;
    private BaseQuickAdapter<UserWatchListBean.ListBean, BaseViewHolder> adapter;

    private List<UserWatchListBean.ListBean> userWatchList;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shopfollow;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        userWatchList = (List<UserWatchListBean.ListBean>) getIntent().getSerializableExtra("userWatchList");
        if (userWatchList!=null){
            followAdapter();
        }
    }

    private void followAdapter() {
        if (adapter==null) {
            adapter = new BaseQuickAdapter<UserWatchListBean.ListBean, BaseViewHolder>
                    (R.layout.item_shop_follow, userWatchList) {

                @Override
                protected void convert(BaseViewHolder baseViewHolder, UserWatchListBean.ListBean dataBean) {
                    GlideApp.with(ShopFollowActivity.this)
                            .load(IntentKey.CAR_IMAGE+dataBean.getLogo())
                            .circleCrop()
                            .into((ImageView) baseViewHolder.getView(R.id.img_shop));
                    baseViewHolder.setText(R.id.tv_shop_name, dataBean.getMerchantName());
                    baseViewHolder.setText(R.id.tv_follow_num, dataBean.getNumberOfPeople() + "人关注");
                    baseViewHolder.getView(R.id.tv_isfollow).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            cancelAttention(dataBean.getMerchantId() + "");
                        }
                    });

                }


            };


            adapter.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                    startActivity(new Intent(getContext(), BusinessActivity.class).putExtra("id",userWatchList.get(position).getMerchantId()+""));

                }
            });
            View emptyview = View.inflate(ShopFollowActivity.this, R.layout.empty_emptylayout, null);
            adapter.setEmptyView(emptyview);
            rvFollow.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }

    }

    /**
     * 取消关注
     */
    private void cancelAttention(String merchantid) {
        EasyHttp.post(this)
                .api(new CancelAttentionApi().setUserId(SPUtil.get(this, IntentKey.USERID,"")).setMerchantid(merchantid)).request(new HttpCallback<PublicMsgBean>(this) {

            @Override
            public void onSucceed(PublicMsgBean data) {
                if (data.getCode()==0) {

                    toast("已取消关注");
                    userWatchList();
                }

            }
            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });

    }
    private void userWatchList() {
        EasyHttp.post(this)
                .api(new UserWatchListApi().setUserId(SPUtil.get(getContext(), IntentKey.USERID, ""))).request(new HttpCallback<UserWatchListBean>(this) {

            @Override
            public void onSucceed(UserWatchListBean data) {
                if (data.getCode()==0) {
                    userWatchList.clear();
                    userWatchList.addAll(data.getList());
                    followAdapter();
                }

            }
            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }


}
